<template>
    <el-dialog :visible.sync="dialog" :title="isAdd ? '新增权限' : '编辑权限'" append-to-body width="500px">
        <el-form ref="form" :model="form" :rules="rules" size="small" label-width="80px">
            <el-form-item label="名称" prop="buttonName">
                <el-input v-model="form.buttonName" onkeyup="value=value.replace(/[^\a-\z\A-\Z]/g,'')" maxlength="25" style="width: 360px;" placeholder="请输入权限名称"  clearable/>
            </el-form-item>
            <el-form-item label="别名" prop="alias">
                <el-input v-model="form.alias" style="width: 360px;" maxlength="20" placeholder="请输入权限别名"  clearable />
            </el-form-item>
            <el-form-item style="margin-bottom: 0px;" label="上级类目">
                <treeselect v-model="form.pid" :options="menus" style="width: 360px;" placeholder="选择上级类目" />
            </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
            <el-button type="text" @click="cancel">取消</el-button>
            <el-button :loading="loading" type="primary" @click="doSubmit">确认</el-button>
        </div>
    </el-dialog>
</template>

<script>
import { add, edit, getMenusTree } from '@/api/menu'
import Treeselect from '@riophae/vue-treeselect'
import '@riophae/vue-treeselect/dist/vue-treeselect.css'
import { validateEmpty } from '@/utils/validate'
export default {
  components: { Treeselect },
  props: {
    isAdd: {
      type: Boolean,
      required: true
    },
    sup_this: {
      type: Object,
      default: null
    }
  },
  data() {
    return {
      loading: false, dialog: false, menus: [],
      form: { buttonName: '', alias: '', pid: -2, isDisable:'0', permissionGrade: '2'},
      rules: {
        buttonName: [
          { required: true, message: '请输入英文名称', trigger: 'blur' }
        ],
        alias: [
          { required: true, message: '请输入权限别名', trigger: 'blur',min:1, },
          {validator:validateEmpty,trigger: 'blur',}
        ]
      }
    }
  },
  methods: {
    cancel() {
      this.resetForm()
    },
    doSubmit() {
      this.$refs['form'].validate((valid) => {
        if (valid) {
          this.loading = true
          if (this.isAdd) {
            this.doAdd()
          } else this.doEdit()
        } else {
          return false
        }
      })
    },
    doAdd() {
      this.form.alias = this.form.alias.trim()
      add(this.form).then(res => {
        if (res.code == 2000) {
          this.resetForm()
          this.$notify({
            title: '添加成功',
            type: 'success',
            duration: 2500
          })
          this.loading = false
          this.$parent.init()
        }
      }).catch(err => {
        this.loading = false
      })
    },
    doEdit() {
      this.form.alias = this.form.alias.trim()
      add(this.form).then(res => {
        if (res.code == 2000) {
          this.resetForm()
          this.$notify({
            title: '修改成功',
            type: 'success',
            duration: 2500
          })
          this.loading = false
          this.$parent.init()
        }
      }).catch(err => {
        this.loading = false
      })
    },
    resetForm() {
      this.dialog = false
      this.$refs['form'].resetFields()
      this.form = { buttonName: '', alias: '', pid: -2, isDisable:'0', permissionGrade: '2',}
    },
    getMenus() {
        getMenusTree().then(res => {
          if (res.code == 2000) {
            this.menus = []
            const menu = { id: -2, label: '顶级类目', children: [] }
            menu.children = res.data
            this.menus.push(menu)
          }
        })
    }
  }
}
</script>

<style scoped>

</style>
